layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
      elem: '#startTime'
    });

    laydate.render({
      elem: '#endTime'
    });

    laydate.render({
      elem: '#startTime2'
        	,value:new Date()
    });

    laydate.render({
      elem: '#endTime2'
        	,value:new Date()
    });

    /**
     * 货运订单表管理
     */
    var OrderTransport = {
        tableId: "orderTransportTable"
    };
    
    /**
     * 调度单表管理
     */
    var TransportDispatch = {
        tableId: "transportDispatchTable"
    };

    /**
     * 初始化表格的列
     */
    OrderTransport.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: '主键'},
            {field: 'customerOrder', sort: true, title: '客户单号',templet: '#customerOrderTpl', style:'font-weight: 600;',width:150},
            {field: 'tstate', sort: true, title: '订单状态',templet: '#tstateTpl',
            	templet: function(d){
            		var str = "";
            		if (d.tstate == '已发运'){
                    	str += '<span style="color:#ffaa00;font-weight: bold;">' + d.tstate + '</span>'
					} else if (d.tstate == '已完成'){
                    	str += '<span style="color:#19be6b;font-weight: bold;">' + d.tstate + '</span>'
					} else if (d.tstate == '作废'){
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.tstate + '</span>'
					} else {
                    	str += '<span style="font-weight: bold;">' + d.tstate + '</span>'
					}
            		return str
            	}
			},
            {field: 'setMentiontime', sort: true, title: '发货日期', style:'font-weight: 600;'},
            {field: 'undertime', sort: true, title: '下单时间'},
            {field: 'createName', title: '制单用户'},
            {field: 'getUnit', title: '卸货单位', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'getLocation', title: '卸货地址', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'getPeople', title: '联系人', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'getTel', title: '联系电话', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'nodes', title: '备注', event: 'checkbox', totalRowText: '合计：'},
            {field: 'kilometer', title: '公里数', event: 'checkbox'},
            {field: 'goodsNum', title: '商品数', totalRow: true},
            {field: 'totalNum', title: '数量', totalRow: true},
            {field: 'totalVolume', title: '体积', totalRow: true},
            {field: 'operateName', sort: true, title: '项目'},
            {field: 'customerName', sort: true, title: '客户'},
            {field: 'transportNumber', sort: true, title: '货运订单号',templet: '#transportNumberTpl', style:'font-weight: 600;'},

            {field: 'lineName', sort: true, title: '路线', style:'font-weight: 600;'},

            {field: 'setUnit', sort: true, title: '装货单位', style:'background-color: #ecf5ff;font-weight: 600;'},
            {field: 'setLocation', sort: true, title: '装货地址', style:'background-color: #ecf5ff;font-weight: 600;'},

            {field: 'createTime', sort: true, title: '制单时间'},
            {field: 'submitTime', sort: true, title: '提交时间111'},
            {field: 'submitName', title: '提交用户'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    OrderTransport.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['tstate'] = $("#tstate").val();
        queryData['operateId'] = $("#operateId").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['startTime2'] = $("#startTime2").val();
        queryData['endTime2'] = $("#endTime2").val();
        queryData['getUnit'] = $("#getUnit").val();
        table.reload(OrderTransport.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OrderTransport.tableId,
        url: Feng.ctxPath + '/orderTransport/dispatchlist',
        page: true,
        limit: 240,
		limits:[60,120,240,360,480,600],
        height: 390,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print'],
        totalRow: true ,//开启合计行
        cellMinWidth: 100,
        cols: OrderTransport.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OrderTransport.search();
    });

    /**
     * 初始化表格的列
     */
    TransportDispatch.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: '主键'},
            {field: 'dispatchNumber', sort: true, title: '运输单号', style:'font-weight: 600;',width: 180},
            {field: 'driver1', title: '司机姓名'},
            {field: 'cartel', title: '电话'},
            {field: 'carnumbger', sort: true, title: '车牌号', totalRowText: '合计'},
            {field: 'transportMoney', sort: true, title: '金额', totalRow: true},
            {field: 'tooDeployVolume', sort: true, title: '体积', totalRow: true},
            {field: 'tooDeployNum', sort: true, title: '数量', totalRow: true},
            {field: 'batchnumber', sort: true, title: '批次', totalRow: true},
            {field: 'goodsName', sort: true, title: '商品名称'},
            {field: 'goodsNumber', sort: true, title: '编号'},
            {field: 'goodsNo', sort: true, title: '货号'},
            {field: 'goodsPacking', title: '包装'},
            {field: 'goodsSpecs', title: '规格'},
            {field: 'createTime', sort: true, title: '配置时间'},
            {field: 'createUserName', title: '创建用户'},
        ]];
    };

    // 单击行事件
    table.on('row(' + OrderTransport.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + OrderTransport.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + OrderTransport.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
        	Detailed(data);
    });

    
    // 工具条点击事件
    table.on('tool(' + OrderTransport.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'launch') {
        	OrderTransport.launch(data);
        } else if (layEvent === 'revoke') {
        	OrderTransport.revoke(data);
        } else if (layEvent === 'detailedSign' || layEvent ==='huoyundetailedSign') {
        	if (data.transportNumber.substring(0,2) != 'CH') {
                layer.open({
                    type: 2,
                    title: '商品明细',
                    area: ['1000px', '500px'],
                    anim: 5,
                    content: Feng.ctxPath + '/orderTransportDetailed?transportNumber=' + data.transportNumber,
                });
			}
        } 
    });
    
    function Detailed(data){
    	var transportNumber;
    	if (data!=null) {
    		transportNumber = data.transportNumber;
		} else {
			transportNumber = 'nodata';
		}

        // 渲染表格
        var tableResult = table.render({
            elem: '#' + TransportDispatch.tableId,
            url: Feng.ctxPath + '/transportDispatch/list?transportNumber='+transportNumber,
            page: true,
            limit: 20,
            height: 370,
        	totalRow: true,
            cellMinWidth: 100,
	        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
	        defaultToolbar: ['filter', 'exports', 'print'],
            cols: TransportDispatch.initColumn()
        });
    };
    
});
